<template>
	<view id="index_cat" class="container">
		<!-- 轮播图 -->
		<view class="banner">
			<swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="5000" :duration="500"
				:circular="true" indicator-color='#FFFFFF'>
				<swiper-item v-for="(item,i) in banner" :key="i">
					<image class="swiper-item" :src="IMG_URL+item.img" mode="aspectFill" @click="jumpArticle(item)">
					</image>
				</swiper-item>
			</swiper>
		</view>
		<!-- 猫咪咨询 -->
		<view class="cat_consult" v-if="consultingList.length">
			<view class="cat_txt">猫咪资讯</view>
			<view class="consult_box">
				<!-- <image :src="IMG_URL+consultingList[0].img" class="c_left" mode="aspectFill"></image> -->
				<view class="c_left"
					:style="{'background':'url('+IMG_URL+consultingList[0].img+') no-repeat','background-size': 'cover'}"
					@click="jumpArticle(consultingList[0])">
					<text>{{consultingList[0].title}}</text>
				</view>
				<view class="c_right">
					<view class="r_img"
						:style="{'background':'url('+IMG_URL+consultingList[1].img+') no-repeat','background-size': 'cover'}"
						@click="jumpArticle(consultingList[1])">
						<text>{{consultingList[1].title}}</text>
					</view>
					<view class="r_img"
						:style="{'background':'url('+IMG_URL+consultingList[2].img+') no-repeat','background-size': 'cover'}"
						@click="jumpArticle(consultingList[2])">
						<text>{{consultingList[2].title}}</text>
					</view>
					<!-- <image :src="IMG_URL+consultingList[1].img" class="r_img" mode="aspectFill"></image> -->
					<!-- <image :src="IMG_URL+consultingList[2].img" class="r_img" mode="aspectFill"></image> -->
				</view>
			</view>
		</view>
		<!-- 最新领养 -->
		<view class="new_consult" v-if="lxmNewList.length">
			<view class="n_title">
				<view class="cat_txt">最新领养</view>
				<view class="more" @click="more">更多</view>
			</view>
			<view class="n_list">
				<view class="n_item" v-for="(item,i) in lxmNewList" :key="i" @click="jumpDetail(item.id)">
					<!-- 宠物图片 -->
					<block v-if="item.img">
						<image class="n_img" :src="IMG_URL+item.img" mode="aspectFit"></image>
					</block>
					<block v-else>
						<image class="n_img" :src="item.cover" mode="aspectFit"></image>
					</block>
					<view class="n_content">
						<!-- 宠物信息 -->
						<view class="n_msg">
							<text class="msg_left">{{item.type}}</text>
							<view class="msg_right">
								<icon
									:class="{'icon_sex_gg' : item.sex==1 ? true :false ,'icon_sex_mm':item.sex==2 ? true :false}">
								</icon>
								<text>{{item.birth}}</text>
							</view>
						</view>
						<!-- 地址 -->
						<view class="n_address">
							<icon class="icon_address"></icon>
							<text>{{item.city}}{{item.district}}</text>
						</view>
						<!-- 点赞评论数 -->
						<view class="n_operation">
							<view class="n_comment">
								<icon class="icon_like"></icon>
								<text>{{item.like}}</text>
							</view>
							<view class="n_comment">
								<icon class="icon_comment"></icon>
								<text>{{item.comment}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 兽医咨询 -->
		<view class="consulting">
			<view class="cat_txt">兽医咨询</view>
			<view class="zixun">
				<view class="zixun-title">猫咪肚子上长猫癣怎么办？</view>
				<view class="zixun-content">
					<text>猫癣，大概养猫的朋友都有遇到过。虽然是小病，但是也不能忽视，处理起来也是比较麻烦和需要耐心，加上他的传染性是可以强大到传染给人，所以一般听到猫癣这个词，大家都觉头疼，那么我们就来说说怎么根治吧……</text>
					<image src="http://nabercat.com/Public/img/lxm/common/docter.jpg" mode=""></image>
				</view>
			</view>
			<!-- <image src="https://nabercat.oss-cn-hangzhou.aliyuncs.com/acat-xcx/consult-img.png" mode="" class="consulting_img"></image> -->
		</view>
		<!-- end -->
		<view class="end">THE END</view>
	</view>
</template>

<script>
	const app = getApp();
	import {
		banners,
		consultingService,
		lxmNew
	} from '@/api/index.js'
	// import {
	// 	IMG_URL
	// } from '@/util/config.js'
	import {
		formatTime
	} from '@/util/time.js'
	export default {
		data() {
			return {
				banner: [], //轮播图
				consultingList: [], //猫咪咨询
				lxmNewList: [], //最新领养
				IMG_URL: app.globalData.IMG_URL, //地址前缀
			}
		},
		methods: {
			// 首页轮播图
			banners() {
				banners().then(res => {
					this.banner = res;
				})
			},

			// 猫咪咨询
			consultingService() {
				consultingService().then(res => {
					this.consultingList = res
					// console.log(res);
				})
			},

			// 最新领养
			lxmNew() {
				lxmNew().then(res => {
					uni.stopPullDownRefresh() //停止刷新
					res.forEach(val => {
						val.birth = formatTime(parseInt(val.birth))
					})
					this.lxmNewList = res
				})
			},

			// 跳转详情页
			jumpDetail(id) {
				uni.navigateTo({
					url: `/pages/detail/detail?id=${id}`
				})
			},

			// 查看更多
			more() {
				uni.switchTab({
					url: '/pages/adopt/adopt'
				});
			},

			// 跳转文章
			jumpArticle(item) {
				const {
					type
				} = item
				if (type == 3) {
					wx.navigateToMiniProgram({
						appId: "wx07d0e5c57b4badbc"
					})
					return false
				}
				if (type == 4) {
					wx.navigateToMiniProgram({
						appId: "wxb1553385bd2505f5"
					})
					return false
				}
				wx.navigateTo({
					url: `/pages/article/article?url=${item.url}`
				})
			},
		},
		onLoad() {
			this.banners(); //首页轮播图
			this.consultingService(); //猫咪咨询
			this.lxmNew(); //最新领养
		},

		// 下拉刷新
		onPullDownRefresh() {
			this.lxmNew(); //最新领养
		},

		// 分享
		onShareAppMessage() {
			return {
				title: '领小咪'
			}
		},
	}
</script>

<style lang="scss">
	#index_cat {

		/* 轮播图 */
		.banner {
			width: 100%;
			height: 350rpx;
			border-radius: 20rpx;
			margin-bottom: 60rpx;

			.swiper {
				width: 100%;
				height: 100%;

				.swiper-item {
					width: 100%;
					height: 100%;
					border-radius: 20rpx;
				}
			}
		}

		/* 猫咪咨询 */
		.cat_consult {
			.consult_box {
				height: 382rpx;
				width: 100%;
				display: flex;
				margin-bottom: 52rpx;

				text {
					font-size: 28rpx;
					color: #FFFFFF;
				}

				.c_left {
					width: 332rpx;
					height: 100%;
					border-radius: 20rpx;
					margin-right: 22rpx;
					background: #F8F8F8;
					padding: 24rpx;
					box-sizing: border-box;
					object-fit: cover;
				}

				.c_right {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.r_img {
						width: 100%;
						height: 180rpx;
						border-radius: 20rpx;
						background: #F8F8F8;
						padding: 24rpx;
						box-sizing: border-box;
					}
				}
			}
		}

		/* 最新领养 */
		.new_consult {
			.n_title {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.more {
					color: #999999;
					font-size: 22rpx;
					padding-right: 15rpx;
					position: relative;

					&::before {
						content: "";
						width: 10rpx;
						height: 15rpx;
						background: url(../../static/image/icon-right.png) no-repeat;
						background-size: cover;
						position: absolute;
						right: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}
			}

			.n_list {
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;

				.n_item {
					width: 332rpx;
					border-radius: 18rpx;
					box-shadow: 0rpx 0rpx 10rpx #D3D3D3;
					margin-bottom: 30rpx;

					.n_img {
						width: 100%;
						height: 428rpx;
						border-radius: 18rpx 18rpx 0 0;
						background: #F8F8F8;
					}

					.n_content {
						padding: 16rpx;

						.n_msg {
							color: #333333;
							font-size: 30rpx;
							font-weight: 600;
							display: flex;
							align-items: center;
							justify-content: space-between;
							margin-bottom: 8rpx;

							.msg_right {
								display: flex;
								align-items: center;
								color: #FFFFFF;
								font-size: 18rpx;

								.icon_sex_gg {
									width: 28rpx;
									height: 28rpx;
									background: url(../../static/image/sex-gg.png) no-repeat;
									background-size: cover;
									margin-right: 8rpx;
								}

								.icon_sex_mm {
									width: 28rpx;
									height: 28rpx;
									background: url(../../static/image/sex-mm.png) no-repeat;
									background-size: cover;
									margin-right: 8rpx;
								}

								&>text {
									height: 28rpx;
									background: rgba(252, 95, 95, 0.08);
									color: #FC8F8F;
									border-radius: 17rpx;
									text-align: center;
									line-height: 28rpx;
									padding: 0 10rpx;
								}
							}
						}

						.n_address {
							font-size: 22rpx;
							color: #999999;
							display: flex;
							align-items: center;
							margin-bottom: 22rpx;

							.icon_address {
								display: block;
								width: 22rpx;
								height: 26rpx;
								background: url(../../static/image/icon-i-address.png) no-repeat;
								background-size: cover;
								margin-right: 10rpx;
							}

							&>text {
								flex: 1;
								overflow: hidden;
								white-space: nowrap;
								text-overflow: ellipsis;
							}
						}

						.n_operation {
							color: #666666;
							font-size: 20rpx;
							display: flex;
							align-items: center;
							justify-content: space-between;

							.n_comment {
								display: flex;
								align-items: center;

								.icon_like {
									width: 30rpx;
									height: 30rpx;
									background: url(../../static/image/like.png) no-repeat;
									background-size: cover;
									margin-right: 7rpx;
								}

								.icon_comment {
									width: 30rpx;
									height: 30rpx;
									background: url(../../static/image/comment.png) no-repeat;
									background-size: cover;
									margin-right: 7rpx;
								}
							}
						}
					}
				}
			}
		}

		/* 兽医咨询 */
		.consulting {
			.zixun {
				color: #FFFFFF;
				font-size: 24rpx;
				padding: 60rpx 45rpx;
				background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/acat-xcx/zixun-bg.png) no-repeat;
				background-size: 100% 100%;

				.zixun-title {
					font-size: 36rpx;
					margin-bottom: 17rpx;
				}

				.zixun-content {
					display: flex;
					align-items: center;

					&>text {
						flex: 1;
						margin-right: 42rpx;
						line-height: 36rpx;
					}

					&>image {
						width: 180rpx;
						height: 180rpx;
					}
				}
			}
		}

		/* 结束 */
		.end {
			margin-top: 48rpx;
			text-align: center;
			color: #888888;
			font-size: 24rpx;
			position: relative;

			&::after {
				content: "";
				width: 118rpx;
				height: 1rpx;
				background: #D8D8D8;
				position: absolute;
				left: 165rpx;
				top: 50%;
				transform: translateY(-50%);
			}

			&::before {
				content: "";
				width: 118rpx;
				height: 1rpx;
				background: #D8D8D8;
				position: absolute;
				right: 165rpx;
				top: 50%;
				transform: translateY(-50%);
			}
		}

		/* 标题样式（页面公用） */
		.cat_txt {
			color: #555555;
			font-weight: 600;
			padding-left: 14rpx;
			position: relative;
			margin-bottom: 16rpx;

			&::after {
				content: "";
				width: 6rpx;
				height: 22rpx;
				background: #FC8F8F;
				border-radius: 3rpx;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
			}
		}
	}
</style>
